/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ARecordEditForm.java
 *
 * Created on 2009-1-18, 14:01:23
 */

package com.guzzservices.easydomain.ui.frame;

import com.guzzservices.easydomain.logic.DomainManager;
import com.guzzservices.easydomain.logic.domain.RecordA;
import com.guzzservices.easydomain.util.ComponentCenter;
import org.jdesktop.application.Action;
import org.jdesktop.application.Task;

/**
 *
 * @author Administrator
 */
public class ARecordEditForm extends javax.swing.JDialog {

    /** Creates new form ARecordEditForm
     * @deprecated 仅供NetBean设计界面使用，程序不可以调用！
     */
    public ARecordEditForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

     public ARecordEditForm(ARecordListFrame frame, boolean isAdd, boolean modal) {
        super(frame, modal);
        this.frame = frame ;
        this.isAdd = isAdd ;

        initComponents();
        initData() ;
    }

    protected void initData(){
        this.topDomainLabel.setText("." + frame.getTopDomain().getFullDomain());

        this.setLocation(frame.getX() + 50, frame.getY() + 95);

        if(isAdd){
            this.setTitle("添加新的二级域名解析") ;
        }else{
            this.setTitle("修改二级域名解析") ;
            this.anameText.setEditable(false) ;
            
            RecordA ra = frame.getSelectedRecordA() ;
            this.selectedRecordA = ra ;
            
            if(ra != null){
                this.anameText.setText(ra.getNameA());
                this.ipText.setText(ra.getIP());
                this.ttlText.setText(ra.getTtl());
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        anameText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        topDomainLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        ipText = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        ttlText = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setLocationByPlatform(true);
        setModal(true);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.guzzservices.easydomain.ui.frame.DDSProjectApp.class).getContext().getResourceMap(ARecordEditForm.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        anameText.setText(resourceMap.getString("anameText.text")); // NOI18N
        anameText.setName("anameText"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        topDomainLabel.setText(resourceMap.getString("topDomainLabel.text")); // NOI18N
        topDomainLabel.setName("topDomainLabel"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        ipText.setText(resourceMap.getString("ipText.text")); // NOI18N
        ipText.setName("ipText"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        ttlText.setEditable(false);
        ttlText.setText(resourceMap.getString("ttlText.text")); // NOI18N
        ttlText.setName("ttlText"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(com.guzzservices.easydomain.ui.frame.DDSProjectApp.class).getContext().getActionMap(ARecordEditForm.class, this);
        jButton1.setAction(actionMap.get("saveFormData")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("cacelEdit")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(64, 64, 64)
                .addComponent(jLabel2)
                .addContainerGap(293, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ipText, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                            .addComponent(ttlText, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(anameText, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(topDomainLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jButton1)
                        .addGap(27, 27, 27)
                        .addComponent(jButton2)
                        .addGap(16, 16, 16)))
                .addGap(133, 133, 133))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(anameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(topDomainLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(ipText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(ttlText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(127, 127, 127))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ARecordEditForm dialog = new ARecordEditForm(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void cacelEdit() {
        this.dispose();
    }

    @Action
    public Task saveFormData() {
        return new SaveFormDataTask(org.jdesktop.application.Application.getInstance(com.guzzservices.easydomain.ui.frame.DDSProjectApp.class));
    }

    private class SaveFormDataTask extends org.jdesktop.application.Task<Object, Void> {
        SaveFormDataTask(org.jdesktop.application.Application app) {
            // Runs on the EDT.  Copy GUI state that
            // doInBackground() depends on from parameters
            // to SaveFormDataTask fields, here.
            super(app);

            ARecordEditForm.this.setVisible(false) ;
        }
        
        @Override protected Object doInBackground() {
            ComponentCenter cc = ComponentCenter.getInstance() ;
            DomainManager dm = DomainManager.getDomainManager() ;
            boolean result ;

            String nameA = anameText.getText() ;
            String IP = ipText.getText() ;
            String ttl = ttlText.getText() ;

            if(isAdd){
                RecordA ra = new RecordA(frame.getTopDomain()) ;
                ra.setIP(IP);
                ra.setNameA(nameA);
                ra.setTtl(ttl);
                
                selectedRecordA = ra ;

                cc.setTextToStatusPanel("保存 " + ra.getVisitedDomain() + " 到域名服务器中……") ;

                result = dm.addRecordA(ra) ;
            }else{
                String orginalNameA = selectedRecordA.getNameA() ;
                String orginalIP = selectedRecordA.getIP() ;
                String orginalTTL = selectedRecordA.getTtl() ;
                

                RecordA ra = new RecordA(selectedRecordA) ;
                ra.setIP(IP);
//                ra.setNameA(nameA); //nameA不允许修改！
                ra.setTtl(ttl);
                
                cc.setTextToStatusPanel("保存 " + ra + " 到域名服务器中……") ;

                result = dm.updateRecordA(orginalNameA, ra) ;
            }

            return Boolean.valueOf(result) ;
        }
        @Override protected void succeeded(Object result) {
           boolean isSuccess = ((Boolean) result).booleanValue() ;
           ComponentCenter cc = ComponentCenter.getInstance() ;

           if(isSuccess){
            cc.setTextToStatusPanel(selectedRecordA.getVisitedDomain() + " 成功更新到域名服务器中。") ;
           }else{
            cc.setTextToStatusPanel(selectedRecordA.getVisitedDomain() + " 更新失败！") ;
           }

           ARecordEditForm.this.dispose();
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField anameText;
    private javax.swing.JTextField ipText;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel topDomainLabel;
    private javax.swing.JTextField ttlText;
    // End of variables declaration//GEN-END:variables

    private ARecordListFrame frame ;
    private boolean isAdd = true ;
    private RecordA selectedRecordA ;
    
}
